package com.epfresh.supplier.activity.standingbook;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.OrderMinusDetailActivity;
import com.epfresh.supplier.activity.OrderMinusRecordActivity;
import com.epfresh.supplier.entity.StandingBookListEntity;
import com.epfresh.supplier.entity.StandingbookDetailEntity;
import com.epfresh.supplier.entity.TotalMoneyEntity;
import com.google.gson.Gson;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shenjie on 2017/9/21.
 */

public class StandingBookDetailActivity extends BaseActivity {

    private TextView customerinfo, menuId, menuStatus, menuTime, customInfo, wait, daikuan, youhui, jianmian, shishou, xianjin, weixin, tuikuan, shezhang;

    private SimpleDateFormat sdf1, sdf2;
    private StandingBookListEntity.StandingBookList entity1;

    private LinearLayout jianmiantuiLl, tuikuanLl;
    private ImageView jianmiantuiIv,tuikuanIv;
    DecimalFormat decimalFormat=new DecimalFormat("0.00");
    private String orderId,debtFee;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        orderId=getIntent().getStringExtra("orderId");
        debtFee=getIntent().getStringExtra("debtFee");
        initView();
        loadHttpRequest();
    }

    private void initView() {
        toolbarTitle.setText("详情");
//        toolbarTitle.setTextColor(getResources().getColor(R.color.light_black_color));
//        float textsize = getResources().getDimension(R.dimen.text_size_max_18);
//        final float scale = getResources().getDisplayMetrics().density;
//        float size = (textsize / scale + 0.5f);
//        toolbarTitle.setTextSize(size);
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        setContentView(R.layout.activity_standingbook_detail);
        menuId = (TextView) findViewById(R.id.menuId);
        menuStatus = (TextView) findViewById(R.id.menustatus);
        menuTime = (TextView) findViewById(R.id.xiadantime);
        customInfo = (TextView) findViewById(R.id.custominfotv);
        wait = (TextView) findViewById(R.id.wait);
        daikuan = (TextView) findViewById(R.id.daikuantv);
        youhui = (TextView) findViewById(R.id.youhuitv);
        jianmian = (TextView) findViewById(R.id.jianmiantuitv);
        shishou = (TextView) findViewById(R.id.shishoutv);
        xianjin = (TextView) findViewById(R.id.xianjintv);
        weixin = (TextView) findViewById(R.id.weixintv);
        tuikuan = (TextView) findViewById(R.id.tuikuantv);
        shezhang = (TextView) findViewById(R.id.shezhangtv);
        jianmiantuiLl = (LinearLayout) findViewById(R.id.jianmiantuill);
        jianmiantuiLl.setOnClickListener(this);
        tuikuanLl = (LinearLayout) findViewById(R.id.tuikuanll);
        tuikuanLl.setOnClickListener(this);
        jianmiantuiIv= (ImageView) findViewById(R.id.jianmiantuiiv);
        tuikuanIv= (ImageView) findViewById(R.id.tuikuaniv);
        entity1 = (StandingBookListEntity.StandingBookList) getIntent().getSerializableExtra("entity");
    }


    private void loadHttpRequest() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_detail);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> maps = new HashMap<>();
        maps.put("storeId", getAppApplication().getStoreId());
        maps.put("orderId", orderId);
        data.setParameters(maps);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_detail, onRequestListener);
    }

    private OnRequestListener<StandingbookDetailEntity> onRequestListener = new OnRequestListener<StandingbookDetailEntity>() {
        @Override
        public StandingbookDetailEntity jsonToObj(String responseStr) {
            hideProgressDialog();
            return new Gson().fromJson(responseStr, StandingbookDetailEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag){
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {

            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StandingbookDetailEntity> response, Object tag) {
            StandingbookDetailEntity entity = response.getResponseElement();

            if (entity != null) {
                menuId.setText("订单编号:" + entity.getOrderNo());
                menuTime.setText(entity.getPlaceOrderTime());
                customInfo.setText(entity.getCustomerName() + " " + entity.getMerAccount());
                wait.setText(decimalFormat.format(new Float(entity.getOrderSumPrice())) + "元");
                daikuan.setText(decimalFormat.format(new Float(entity.getProductTotalPrice())) + "元");
                if(new Float(entity.getCoupons())!=0){
                    youhui.setText("-"+decimalFormat.format(new Float(entity.getCoupons()))+"元");
                }else {
                    youhui.setText(decimalFormat.format(new Float(entity.getCoupons()))+"元");
                }

                if(new Float(entity.getJmtTotlePrice())!=0){
                    jianmian.setText("-"+decimalFormat.format(new Float(entity.getJmtTotlePrice()))+"元");
                }else {
                    jianmian.setText(decimalFormat.format(new Float(entity.getJmtTotlePrice()))+"元");
                }

                if(entity.getMerFactPaidTotalPrice()!=null){
                    shishou.setText(decimalFormat.format(new Float(entity.getMerFactPaidTotalPrice())) + "元");
                }else {
                    shishou.setText( "0.00元");
                }

                if(entity.getCashFee()!=null){
                    xianjin.setText(decimalFormat.format(new Float(entity.getCashFee())) + "元");
                }else {
                    xianjin.setText( "0.00元");
                }


                if(entity.getJmtTotlePrice()!=null&&new Float(entity.getJmtTotlePrice())==0){
                    jianmiantuiLl.setClickable(false);
                    jianmiantuiIv.setVisibility(View.INVISIBLE);
                }

                if(entity.getRefundPrice()!=null&&new Float(entity.getRefundPrice())==0){
                    tuikuanLl.setClickable(false);
                    tuikuanIv.setVisibility(View.INVISIBLE);
                }

                if(entity.getOnlineFee()!=null){
                    weixin.setText(decimalFormat.format(new Float(entity.getOnlineFee()))+ "元");
                }else {
                    weixin.setText("0.00元");
                }

                if(new Float(entity.getRefundPrice())>0){
                    tuikuan.setText("-"+decimalFormat.format(new Float(entity.getRefundPrice()))+"元");
                }else {
                    tuikuan.setText(decimalFormat.format(new Float(entity.getRefundPrice()))+"元");
                }

                shezhang.setText(decimalFormat.format(new Float(entity.getBePayPrice())) + "元");
                if (new Float(entity.getBePayPrice()) != 0) {
                    menuStatus.setText("未结清");
                } else {
                    menuStatus.setText("已结清");
                }

            }
        }
    };

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.jianmiantuill:
            case R.id.tuikuanll:
                Intent detail = new Intent(this, OrderMinusRecordActivity.class);
                detail.putExtra("id", orderId + "");
                startActivity(detail);
                break;

        }
    }
}
